'''''
服务端，发布模式
'''
import zmq
from random import randrange
import time

from mytest.quote_service import quote_pb2

'''
data = {
    # 交易日 string
    "tradingDay": "2019-10-17",
    # 合约代码 tring
    "symbol": "000001",
    # 交易所代码 string
    "market": "XSHE",
    # 最新价 double
    "lastPrice": 0.01,
    # 上次结算价 double
    "preSettlementPrice": 0.01,
    # 昨收盘 double
    "preClosePrice": 0.01,
    # 昨持仓量 double
    "preOpenInterest": 10000.01,
    # 今开盘 double
    "openPrice": 0.01,
    # 最高价 double
    "highPrice": 0.01,
    # 最低价 double
    "lowPrice": 0.01,
    # 总成交数量 uint32
    "totalVolume": 320000000,
    # 总成交金额 double
    "totalValue": 1000000000.01,
    # 持仓量 uint64
    "openInterest": 640000000,
    # 今收盘 double
    "closePrice": 0.01,
    # 本次结算价 double
    "settlementPrice": 0.01,
    # 涨停板价 double
    "UpperLimitPrice": 0.01,
    # 跌停板价 double
    "LowerLimitPrice": 0.01,
    # 申买价 repeated double
    "bidPriceList": [0.01],
    # 申买量 repeated int32
    "bidVolumeList": [32],
    # 申卖价 repeated double
    "askPriceList": [0.01],
    # 申卖量 repeated int32
    "askVolumeList": [32],
    # 当日均价 double
    "averagePrice": 0.01,
    # 成交数量 uint32
    "volume": 320000000,
    # 市盈率1 double
    "peRatio1": 0.01,
    # 市盈率2 double
    "peRatio2": 0.01,
    # 时间戳 uint64 毫秒
    "timeStamp": 1571291839,
    # 产品实时状态 string  第1位：
    # ‘S’表示启动（开市前）时段，
    # ‘C’表示集合竞价时段，
    # ‘T’表示连续交易时段，
    # ‘B’表示休市时段，
    # ‘E’表示闭市时段，
    # ‘P’表示产品停牌。第2位：
    # ‘0’表示未连续停牌，
    # ‘1’表示连续停牌。无意义填空格。
    "phaseCode": "S",
    # t-1基金净值 double
    "preCloseIOPV": 0.01,
    # 基金净值 double
    "iOPV": 0.01,

}
'''

# 为 StockTick 填充数据
stock_tick = quote_pb2.StockTick()
stock_tick.tradingDay = "2019-10-17"
stock_tick.symbol = "000001"
stock_tick.market = "XSHE"
stock_tick.lastPrice = 0.01
stock_tick.preSettlementPrice = 0.01
stock_tick.preClosePrice = 0.01
stock_tick.preOpenInterest = 10000.01
stock_tick.openPrice = 0.01
stock_tick.highPrice = 0.01
stock_tick.lowPrice = 0.01
stock_tick.totalVolume = 320000000
stock_tick.totalValue = 1000000000.01
stock_tick.openInterest = 640000000
stock_tick.closePrice = 0.01
stock_tick.settlementPrice = 0.01
stock_tick.UpperLimitPrice = 0.01
stock_tick.LowerLimitPrice = 0.01
stock_tick.bidPriceList.extend([0.01,0.02])
stock_tick.bidVolumeList.extend([32,33,34])
stock_tick.askPriceList.extend([0.01,0.02])
stock_tick.askVolumeList.extend([32,33,34])
stock_tick.averagePrice = 0.01
stock_tick.volume = 320000000
stock_tick.peRatio1 = 0.01
stock_tick.peRatio2 = 0.01
stock_tick.timeStamp = 1571291839
stock_tick.phaseCode = "S"
stock_tick.preCloseIOPV = 0.01
stock_tick.iOPV = 0.01

# 对数据进行序列化
res = stock_tick.SerializeToString()

# print(res)
# 对已经序列化的数据进行反序列化
# stock_tick2 = quote_pb2.StockTick()
# stock_tick2.ParseFromString(res)
# print(stock_tick2)

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://127.0.0.1:9999")

while True:
    # zipcode = randrange(1, 100000)
    # temperature = randrange(-80, 135)
    # relhumidity = randrange(10, 60)
    # print("%i %i %i" % (zipcode, temperature, relhumidity))
    # socket.send(("%i %i %i" % (zipcode, temperature, relhumidity)).encode('utf-8'))
    # socket.send(b"acequant.marketdata.stock.tick ['b','c']")
    message = res
    socket.send_multipart([b'acequant.marketdata.stock.tick', message])
    time.sleep(1)
